package com.bigdata.service.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.bigdata.common.annotation.RepeatSubmit;
import com.bigdata.common.annotation.Log;
import com.bigdata.common.core.controller.BaseController;
import com.bigdata.common.core.domain.PageQuery;
import com.bigdata.common.core.domain.R;
import com.bigdata.common.core.validate.AddGroup;
import com.bigdata.common.core.validate.EditGroup;
import com.bigdata.common.core.validate.QueryGroup;
import com.bigdata.common.enums.BusinessType;
import com.bigdata.common.utils.poi.ExcelUtil;
import com.bigdata.service.domain.vo.PointRulesVo;
import com.bigdata.service.domain.bo.PointRulesBo;
import com.bigdata.service.service.IPointRulesService;
import com.bigdata.common.core.page.TableDataInfo;

/**
 * 积分规则配置
 *
 * @author ruoyi
 * @date 2025-07-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/service/rules")
public class PointRulesController extends BaseController {

    private final IPointRulesService iPointRulesService;

    /**
     * 查询积分规则配置列表
     */
    @SaCheckPermission("service:rules:list")
    @GetMapping("/list")
    public TableDataInfo<PointRulesVo> list(PointRulesBo bo, PageQuery pageQuery) {
        return iPointRulesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出积分规则配置列表
     */
    @SaCheckPermission("service:rules:export")
    @Log(title = "积分规则配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PointRulesBo bo, HttpServletResponse response) {
        List<PointRulesVo> list = iPointRulesService.queryList(bo);
        ExcelUtil.exportExcel(list, "积分规则配置", PointRulesVo.class, response);
    }

    /**
     * 获取积分规则配置详细信息
     *
     * @param ruleId 主键
     */
    @SaCheckPermission("service:rules:query")
    @GetMapping("/{ruleId}")
    public R<PointRulesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String ruleId) {
        return R.ok(iPointRulesService.queryById(ruleId));
    }

    /**
     * 新增积分规则配置
     */
    @SaCheckPermission("service:rules:add")
    @Log(title = "积分规则配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PointRulesBo bo) {
        return toAjax(iPointRulesService.insertByBo(bo));
    }

    /**
     * 修改积分规则配置
     */
    @SaCheckPermission("service:rules:edit")
    @Log(title = "积分规则配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PointRulesBo bo) {
        return toAjax(iPointRulesService.updateByBo(bo));
    }

    /**
     * 删除积分规则配置
     *
     * @param ruleIds 主键串
     */
    @SaCheckPermission("service:rules:remove")
    @Log(title = "积分规则配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ruleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ruleIds) {
        return toAjax(iPointRulesService.deleteWithValidByIds(Arrays.asList(ruleIds), true));
    }
}
